package com.jzr.board.service

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import com.blankj.utilcode.utils.LogUtils
import com.jzr.board.base.Constant
import com.jzr.board.ui.fragment.main.PatientListFragment
import com.jzr.board.ui.voip.VoipAudioRingingActivity
import com.jzr.board.ui.voip.VoipRingingActivity
import com.jzr.board.utils.CommonUtil
import com.jzr.board.utils.PreferUtil
import com.jzr.board.utils.rtc.AEvent
import com.jzr.board.utils.rtc.AEvent.removeListener
import com.jzr.board.utils.rtc.IEventListener
import com.jzr.board.utils.rtc.listener.XHLoginManagerListener
import com.jzr.board.utils.rtc.listener.XHVoipManagerListener
import com.jzr.board.utils.rtc.listener.XHVoipP2PManagerListener
import com.starrtc.starrtcsdk.api.XHClient
import com.starrtc.starrtcsdk.api.XHCustomConfig
import com.starrtc.starrtcsdk.apiInterface.IXHResultCallback

class KeepLiveService : Service(), IEventListener {

    private var isLogin = false

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        initSDK()
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        removeListener()
    }

    private fun initSDK() {
        initFree()
    }

    private fun initFree() {
        isLogin = XHClient.getInstance().isOnline
        if (!isLogin) {
            if (Constant.userId == "") {
                Constant.userId = PreferUtil.getInstance().bedCode
                PreferUtil.getInstance().userId = Constant.userId
            }
            addListener()
            val customConfig =
                XHCustomConfig.getInstance(this)
            customConfig.chatroomServerUrl =  Constant.CHATROOM_SERVER_URL
            customConfig.liveSrcServerUrl = Constant.LIVE_SRC_SERVER_URL
            customConfig.liveVdnServerUrl =Constant.LIVE_VDN_SERVER_URL
            customConfig.setImServerUrl(Constant.IM_SERVER_URL)
            customConfig.voipServerUrl =  Constant.VOIP_SERVER_URL
            //customConfig.setLogEnable(false); //关闭SDK调试日志
//            customConfig.setDefConfigOpenGLESEnable(false);
//            customConfig.setDefConfigCameraId(1);//设置默认摄像头方向  0后置  1前置
//            customConfig.setDefConfigVideoSize(XHConstants.XHCropTypeEnum.STAR_VIDEO_CONFIG_360BW_640BH_180SW_320SH);
//            customConfig.setLogDirPath(Environment.getExternalStorageDirectory().getPath()+"/starrtcLog");
//            customConfig.setDefConfigCamera2Enable(false);
//            StarCamera.setFrameBufferEnable(false);
            customConfig.initSDKForFree(
                Constant.userId,
                { errMsg, data ->
//                    MLOC.e("KeepLiveService", "error:$errMsg")
//                    MLOC.showMsg(this@KeepLiveService, errMsg)
                },
                Handler()
            )
//            XHClient.getInstance().chatManager
//                .addListener(XHChatManagerListener())
//            XHClient.getInstance().groupManager
//                .addListener(XHGroupManagerListener())
            XHClient.getInstance().voipManager
                .addListener(XHVoipManagerListener())
            XHClient.getInstance().voipP2PManager
                .addListener(XHVoipP2PManagerListener())
            XHClient.getInstance().loginManager
                .addListener(XHLoginManagerListener())
//            XHVideoSourceManager.getInstance().videoSourceCallback = DemoVideoSourceCallback()
            XHClient.getInstance().loginManager
                .loginFree(object : IXHResultCallback {
                    override fun success(data: Any?) {
                        LogUtils.e("KeepLiveService", "loginSuccess")
                        isLogin = true
                    }

                    override fun failed(errMsg: String?) {
                        LogUtils.e("KeepLiveService", "loginFailed $errMsg")
//                        MLOC.showMsg(this@KeepLiveService, errMsg)
                    }
                })
        }
    }


    private fun addListener() {
        AEvent.addListener(AEvent.AEVENT_LOGOUT, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_CALLING, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_CALLING_AUDIO, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_P2P_REV_CALLING, this)
        AEvent.addListener(AEvent.AEVENT_C2C_REV_MSG, this)
        AEvent.addListener(AEvent.AEVENT_REV_SYSTEM_MSG, this)
        AEvent.addListener(AEvent.AEVENT_GROUP_REV_MSG, this)
        AEvent.addListener(AEvent.AEVENT_USER_KICKED, this)
        AEvent.addListener(AEvent.AEVENT_CONN_DEATH, this)
    }

    private fun removeListener() {
        removeListener(AEvent.AEVENT_LOGOUT, this)
        removeListener(AEvent.AEVENT_VOIP_REV_CALLING, this)
        removeListener(AEvent.AEVENT_VOIP_REV_CALLING_AUDIO, this)
        removeListener(AEvent.AEVENT_VOIP_P2P_REV_CALLING, this)
        removeListener(AEvent.AEVENT_C2C_REV_MSG, this)
        removeListener(AEvent.AEVENT_REV_SYSTEM_MSG, this)
        removeListener(AEvent.AEVENT_GROUP_REV_MSG, this)
        removeListener(AEvent.AEVENT_USER_KICKED, this)
        removeListener(AEvent.AEVENT_CONN_DEATH, this)
    }

    override fun dispatchEvent(aEventID: String?, success: Boolean, eventObj: Any?) {
        when(aEventID){
            AEvent.AEVENT_VOIP_REV_CALLING->{
//                PatientListFragment.patientListAll
                val intent = Intent(this, VoipRingingActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("targetId", eventObj.toString())
                startActivity(intent)

            }

            AEvent.AEVENT_VOIP_REV_CALLING_AUDIO->{
                val intent = Intent(this, VoipAudioRingingActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("targetId", eventObj.toString())
                startActivity(intent)
            }

        }
    }

}